package com.chris.homecare.components
{
	import com.chris.homecare.data.EventData;
	import com.chris.homecare.utils.CommonUtils;
	
	import feathers.controls.Label;
	import feathers.controls.LayoutGroup;
	import feathers.controls.ScrollContainer;
	import feathers.layout.AnchorLayout;
	import feathers.layout.AnchorLayoutData;
	
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import starling.display.Quad;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	[Event(name="showDetailEvent",type="starling.events.Event")]
	
	public class EventComponent extends LayoutGroup
	{
		private var title:String;
		private var location:String;
		private var organiser:String;
		private var eventObject:EventData;
		
		private var background:Quad;

		private var topBorder:Quad;
		private var bottomBorder:Quad;
		
		protected var touchPointID:int = -1;
		private static const HELPER_TOUCHES_VECTOR:Vector.<Touch> = new <Touch>[];
		private static const HELPER_POINT:Point = new Point();
		
		private var timer:Timer;
		
		public static const SHOW_DETAIL_EVENT:String = "showDetailEvent";
		
		public function EventComponent(event:EventData)
		{
			this.title = event.titel;
			this.location = event.location;
			this.organiser = event.organiser;
			this.eventObject = event;
			
			this.addEventListener(TouchEvent.TOUCH, touchHandler);
		}
		
		override protected function initialize():void
		{
			this.layout = new AnchorLayout();
			
			background = new Quad(10,200, 0xEEEEEE);
			addChild(background);
			
			var titleLabel:Label = new Label();
			titleLabel.x = titleLabel.y = 5;
			titleLabel.nameList.add("event-heading-lbl");
			titleLabel.text = title;
			addChild(titleLabel);
			
			var orgainiserLabel:Label = new Label();
			orgainiserLabel.text = "Time: " +organiser;
			orgainiserLabel.nameList.add("form-heading-lbl");
			addChild(orgainiserLabel);
			
			var organiserLayoutData:AnchorLayoutData = new AnchorLayoutData();
			organiserLayoutData.topAnchorDisplayObject = titleLabel;
			organiserLayoutData.top = 15;
			organiserLayoutData.left = 5;
			orgainiserLabel.layoutData = organiserLayoutData;
			
			var locationLabel:Label = new Label();
			locationLabel.text = "Location: " +location;
			locationLabel.nameList.add("form-heading-lbl");
			addChild(locationLabel);
			
			var locationLayoutData:AnchorLayoutData = new AnchorLayoutData();
			locationLayoutData.topAnchorDisplayObject = orgainiserLabel;
			locationLayoutData.top = locationLayoutData.left = 5;
			locationLabel.layoutData = locationLayoutData;
			
			bottomBorder = new Quad(2,1,0x000000);
			addChild(bottomBorder);
			
			super.initialize();
		}
		
		override protected function draw():void
		{
			this.height = background.height = 140*CommonUtils.scaleValue;
			background.width = bottomBorder.width = stage.stageWidth;
			
			bottomBorder.y = 140*CommonUtils.scaleValue - 1;
			
			super.draw();
		}
		
		
		protected function touchHandler(event:TouchEvent):void
		{
			if(!this._isEnabled)
			{
				return;
			}
			
			const touches:Vector.<Touch> = event.getTouches(this, null, HELPER_TOUCHES_VECTOR);
			if(touches.length == 0)
			{
				//end of hover
				return;
			}
			if(this.touchPointID >= 0)
			{
				var touch:Touch;
				for each(var currentTouch:Touch in touches)
				{
					if(currentTouch.id == this.touchPointID)
					{
						touch = currentTouch;
						break;
					}
				}
				
				if(!touch)
				{
					//end of hover
					HELPER_TOUCHES_VECTOR.length = 0;
					return;
				}
				
				if(touch.phase == TouchPhase.ENDED)
				{
					this.touchPointID = -1;
					touch.getLocation(this, HELPER_POINT);
					var isInBounds:Boolean = this.hitTest(HELPER_POINT, true) != null;
					if(isInBounds && !ScrollContainer(this.parent.parent.parent).isScrolling)
					{
						background.alpha = 0.75;
						timer = new Timer(100,2);
						timer.addEventListener(TimerEvent.TIMER, onTimerComplete);
						timer.start();
						
						this.dispatchEventWith(SHOW_DETAIL_EVENT, true, eventObject);
					}
				}
			}
			else //if we get here, we don't have a saved touch ID yet
			{
				for each(touch in touches)
				{
					if(touch.phase == TouchPhase.BEGAN)
					{
						this.touchPointID = touch.id;
						break;
					}
				}
			}
			HELPER_TOUCHES_VECTOR.length = 0;
		}
		
		protected function onTimerComplete(event:TimerEvent):void
		{
			background.alpha = 1;
			timer.removeEventListener(TimerEvent.TIMER, onTimerComplete);
			timer = null;
		}
	}
}